package com.gitee.zycra.future.job.manager;

import com.gitee.zycra.future.job.common.bo.JobTriggerBO;
import com.gitee.zycra.future.job.common.bo.PageQuery;
import com.gitee.zycra.future.job.common.enums.ScheduleRatioEnum;
import com.gitee.zycra.future.job.common.model.JobRunning;
import com.gitee.zycra.future.job.common.param.PageQueryRunningParam;
import com.gitee.zycra.future.job.common.util.DateUtil;
import com.gitee.zycra.future.job.common.util.IdGenerator;
import com.gitee.zycra.future.job.dao.JobRunningDao;
import com.gitee.zycra.jdbc.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zycra
 */
@Component
public class JobRunningManager {

    @Autowired
    private JobRunningDao jobRunningDao;

    @Autowired
    private IdGenerator idGenerator;

    /**
     * 插入任务信息
     *
     * @param jobRunning 任务信息对象
     * @return 插入条数
     */
    public int insertJobRunning(JobRunning jobRunning) {
        jobRunning.setId(idGenerator.nextId());
        return jobRunningDao.insertJobRunning(jobRunning);
    }

    /**
     * 根据主键查询任务信息
     *
     * @param id 主键
     * @return 任务信息
     */
    public JobRunning selectById(Long id) {
        return jobRunningDao.selectById(id);
    }

    /**
     * 根据任务调度ID查询
     *
     * @param pageQuery 查询参数
     * @return 查询到的数据
     */
    public PageResult<JobRunning> selectJobRunning(PageQuery<PageQueryRunningParam> pageQuery) {
        return jobRunningDao.selectJobRunning(pageQuery);
    }

    /**
     * 查询待调度任务
     *
     * @return 待调度任务
     */
    public List<JobTriggerBO> selectJobForTrigger() {
        return jobRunningDao.selectJobForTrigger();
    }

    /**
     * 手动取消
     *
     * @param jobRunning 执行信息
     * @return 影响条数
     */
    public int manualCancel(JobRunning jobRunning) {
        LocalDateTime now = DateUtil.getNowLocalDateTime();
        if (jobRunning.getNodeIp() == null) {
            return jobRunningDao.updateJobRunning(new JobRunning().setId(jobRunning.getId())
                    .setScheduleRatio(ScheduleRatioEnum.CANCELED.getCode())
                    .setScheduleTime(now)
                    .setCompleteTime(now));
        }
        return jobRunningDao.updateJobRunning(new JobRunning().setId(jobRunning.getId())
                .setScheduleRatio(ScheduleRatioEnum.CANCELED.getCode())
                .setScheduleTime(now));
    }

    /**
     * 根据ID更新
     *
     * @param jobRunning 执行信息
     * @return 更新条数
     */
    public int updateJobRunning(JobRunning jobRunning) {
        return jobRunningDao.updateJobRunning(jobRunning);
    }

    /**
     * 查询待转移的任务
     *
     * @return 待转移的任务
     */
    public List<JobRunning> selectForTransfer() {
        return jobRunningDao.selectForTransfer();
    }

    /**
     * 根据调度ID查询
     *
     * @param jobScheduleId 调度ID
     * @return 执行信息
     */
    public JobRunning selectByJobScheduleId(Long jobScheduleId) {
        return jobRunningDao.selectByJobScheduleId(jobScheduleId);
    }

    /**
     * 根据ID删除
     *
     * @param idList id集合
     * @return 删除行数
     */
    public int deleteRunning(List<Long> idList) {
        return jobRunningDao.deleteRunning(idList);
    }

    /**
     * 检查超时任务
     *
     * @return 影响行数
     */
    public int checkTimeoutAndUpdate() {
        return jobRunningDao.checkTimeoutAndUpdate();
    }
}
